using System;
using System.Windows.Forms;
using ZoneFiveSoftware.Common.Data.Fitness;
using FilteredStatistics.Common;
using FilteredStatistics.Common.Controller;
using FilteredStatistics.Data;

namespace FilteredStatistics.View
{
    class CustomizableCriteriasManager
    {
        private CustomizableCriteriasManager()
        {
            FilterCriteriaController.Instance.AvailableCriteriasChanged += new System.ComponentModel.PropertyChangedEventHandler(FilterCriteriaController_AvailableCriteriasChanged);

            SetupCriteriaSelectedEvents();
        }

        private void FilterCriteriaController_AvailableCriteriasChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            SetupCriteriaSelectedEvents();
        }

        private void SetupCriteriaSelectedEvents()
        {
            if (m_SplitsCriteria != null)
            {
                m_SplitsCriteria.SplitsCriteriaSelected -= new SplitsPlaceholderFilterCriteria.SplitsCriteriaSelectedEventHandler(SplitsPlaceholderCriteria_SplitsCriteriaSelected);
            }

            if (m_TemplateSplitsCriteria != null)
            {
                m_TemplateSplitsCriteria.SplitsCriteriaSelected -= new TemplateSplitsPlaceholderFilterCriteria.SplitsCriteriaSelectedEventHandler(TemplateSplitsPlaceholderCriteria_SplitsCriteriaSelected);
            }

            if (m_PostProcessCriteria != null)
            {
                m_PostProcessCriteria.PostProcessCriteriaSelected -= new PostProcessPlaceholderFilterCriteria.PostProcessCriteriaSelectedEventHandler(PostProcessPlaceholderCriteria_PostProcessCriteriaSelected);
            }

            m_SplitsCriteria = FilterCriteriaController.Instance.GetCriteria(GUIDs.SplitsCriteriaGuid) as SplitsPlaceholderFilterCriteria;
            m_TemplateSplitsCriteria = FilterCriteriaController.Instance.GetCriteria(GUIDs.SplitsCriteriaGuid) as TemplateSplitsPlaceholderFilterCriteria;
            m_PostProcessCriteria = FilterCriteriaController.Instance.GetCriteria(GUIDs.PostProcessCriteriaGuid) as PostProcessPlaceholderFilterCriteria;

            if (m_SplitsCriteria != null)
            {
                m_SplitsCriteria.SplitsCriteriaSelected += new SplitsPlaceholderFilterCriteria.SplitsCriteriaSelectedEventHandler(SplitsPlaceholderCriteria_SplitsCriteriaSelected);
            }

            if (m_TemplateSplitsCriteria != null)
            {
                m_TemplateSplitsCriteria.SplitsCriteriaSelected += new TemplateSplitsPlaceholderFilterCriteria.SplitsCriteriaSelectedEventHandler(TemplateSplitsPlaceholderCriteria_SplitsCriteriaSelected);
            }

            if (m_PostProcessCriteria != null)
            {
                m_PostProcessCriteria.PostProcessCriteriaSelected += new PostProcessPlaceholderFilterCriteria.PostProcessCriteriaSelectedEventHandler(PostProcessPlaceholderCriteria_PostProcessCriteriaSelected);
            }
        }

        private void TemplateSplitsPlaceholderCriteria_SplitsCriteriaSelected(TemplateSplitsPlaceholderFilterCriteria criteria, IFilterCriteria previousCriteria, out IFilterCriteria resultCriteria)
        {
            // The user selected a splits criteria, ask for which type
            OptionsSplitsCriteriaSelectionDialog dlg = new OptionsSplitsCriteriaSelectionDialog(m_Activity);

            dlg.PreviousCriteria = previousCriteria;
            dlg.ShowDialog();

            if (dlg.DialogResult == DialogResult.OK)
            {
                resultCriteria = dlg.GetSelectedCriteria();
            }
            else
            {
                resultCriteria = previousCriteria;
            }
        }

        private void SplitsPlaceholderCriteria_SplitsCriteriaSelected(SplitsPlaceholderFilterCriteria placeholderCriteria, IFilterCriteria previousCriteria, out IFilterCriteria resultCriteria)
        {
            // The user selected a splits criteria, ask for which type
            SplitsCriteriaSelectionDialog dlg = new SplitsCriteriaSelectionDialog(m_Activity, placeholderCriteria);

            dlg.PreviousCriteria = previousCriteria;
            dlg.ShowDialog();

            if (dlg.DialogResult == DialogResult.OK)
            {
                resultCriteria = dlg.SelectedCriteria;
            }
            else
            {
                resultCriteria = previousCriteria;
            }
        }

        private void PostProcessPlaceholderCriteria_PostProcessCriteriaSelected(PostProcessPlaceholderFilterCriteria placeholderCriteria, IFilterCriteria previousCriteria, out IFilterCriteria resultCriteria)
        {
            PostProcessFilterSetupDialog dlg = new PostProcessFilterSetupDialog(m_Activity);

            dlg.ConfiguredCriteria = previousCriteria;
            dlg.ShowDialog();

            if (dlg.DialogResult == DialogResult.OK)
            {
                resultCriteria = dlg.ConfiguredCriteria;
            }
            else
            {
                resultCriteria = previousCriteria;
            }
        }

        public static CustomizableCriteriasManager Instance
        {
            get
            {
                if (m_Instance == null)
                {
                    m_Instance = new CustomizableCriteriasManager();
                }

                return m_Instance;
            }
        }

        public IActivity Activity
        {
            set
            {
                m_Activity = value;
            }
        }

        private static CustomizableCriteriasManager m_Instance = null;

        private IActivity m_Activity = null;
        private SplitsPlaceholderFilterCriteria m_SplitsCriteria = null;
        private TemplateSplitsPlaceholderFilterCriteria m_TemplateSplitsCriteria = null;
        private PostProcessPlaceholderFilterCriteria m_PostProcessCriteria = null;
    }
}
